﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LikeUs.WebService.Client;
using LikeUs.WebService.Client.WSInfoClient;

namespace LikeUsWebSite
{
    public partial class UpdateProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            m_messageLabel.Text = String.Empty;
            if (!Page.IsPostBack) //being loaded for first time
            {
                getUserInfo();
            }
        }

        protected void getUserInfo()
        {
            WSInfoProxy wsProxy = new WSInfoProxy();
            //UserData userData = wsProxy.GetMyUser(Page.User.Identity.Name);
            UserData userData = (UserData)Session["userData"];
            m_userName.Text = userData.Name1;
            m_email.Text = userData.Email;
            m_userImage.ImageUrl = userData.Image;
        }

        protected void m_submitProfUpdate_Click(object sender, EventArgs e)
        {
            WSInfoProxy wsProxy = new WSInfoProxy();
            //UserData userData = wsProxy.GetMyUser(((UserData)Session["userData"]).UserName);
            UserData userData = (UserData)Session["userData"];
            if (IsValidEmail(m_email.Text) == false)
            {
                m_messageLabel.Text = "Please enter a valid email address.";
                return;
            }
            userData.Email = m_email.Text;
            userData.RequestedDistance = float.Parse(m_ReqDistList.SelectedValue);
            if (m_pwTextBox.Text.CompareTo(String.Empty) != 0)
            {
                if (m_valPWTextBox.Text.CompareTo(String.Empty) != 0)
                {
                    if (m_valPWTextBox.Text.CompareTo(m_pwTextBox.Text) == 0)
                    {
                        userData.Password = m_valPWTextBox.Text;
                    }
                    else
                    {
                        m_messageLabel.Text = "The new password validation is not correct";
                        return;
                    }
                }
                else
                {
                    m_messageLabel.Text = "Please validate you new password.";
                    return;
                }
            }
            wsProxy.UpdateUserData(userData);

            Response.Redirect("Profile.aspx");
        }        

        private bool IsValidEmail(string email)
        {
            try
            {
                System.Net.Mail.MailAddress addr = new System.Net.Mail.MailAddress(email);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}